package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PhotoAlbumBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.PhotoAlbumQTO;
import com.yoga.dao.dao.PhotoAlbumDao;
import com.yoga.dao.domain.PhotoAlbumDO;
import com.yoga.manager.admin.AdminPhotoAlbumManager;
import com.yoga.manager.beanUtil.PhotoAlbumBeanUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/10/20.
 */
@Component
public class AdminPhotoAlbumManagerImpl implements AdminPhotoAlbumManager {
    Logger logger = LoggerFactory.getLogger(AdminPhotoAlbumManagerImpl.class);

    @Resource
    private PhotoAlbumDao photoAlbumDao;

    @Override
    public PhotoAlbumBO addData(PhotoAlbumBO photoAlbumBO) throws AppException {
        if (photoAlbumBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(photoAlbumBO.getPhotoUrl())) {
            throw new AppException(AppErrorCodeEnum.PHOTO_NULL);
        }
        if (StringUtils.isEmpty(photoAlbumBO.getTitleName())) {
            throw new AppException(AppErrorCodeEnum.TITLE_NULL);
        }
        if (photoAlbumBO.getPhotoType() == null) {
            throw new AppException(AppErrorCodeEnum.TYPE_NULL);
        }
        PhotoAlbumDO insertData = PhotoAlbumBeanUtil.photoAlbumBOToDO(photoAlbumBO);
        if (insertData.getPhotoType() == 2) {
            if (insertData.getId() != null) {
                int num = photoAlbumDao.updateById(insertData);
            } else {
                int num = photoAlbumDao.insert(insertData);
            }
            return PhotoAlbumBeanUtil.photoAlbumDOToBO(photoAlbumDao.findById(insertData.getId()));
        }

        int num = photoAlbumDao.insert(insertData);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }

        return PhotoAlbumBeanUtil.photoAlbumDOToBO(photoAlbumDao.findById(insertData.getId()));
    }

    @Override
    public Boolean updateData(PhotoAlbumBO photoAlbumBO) throws AppException {
        if (photoAlbumBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (photoAlbumBO.getPhotoAlbumId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (photoAlbumBO.getPhotoType() == 2) {//不可修改轮播图
            throw new AppException(AppErrorCodeEnum.PHOTO_TYPE_2);
        }
        int num = photoAlbumDao.updateById(PhotoAlbumBeanUtil.photoAlbumBOToDO(photoAlbumBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public Boolean deleteById(Long photoAlbumId) throws AppException {
        if (photoAlbumId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        int num = photoAlbumDao.deleteById(photoAlbumId);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public PhotoAlbumBO findById(Long photoAlbumId) throws AppException {
        if (photoAlbumId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        PhotoAlbumBO photoAlbumBO = PhotoAlbumBeanUtil.photoAlbumDOToBO(photoAlbumDao.findById(photoAlbumId));

        return photoAlbumBO;
    }

    @Override
    public PageBO<PhotoAlbumBO> findForPage(PhotoAlbumQTO photoAlbumQTO) throws AppException {
        if (photoAlbumQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (photoAlbumQTO.getPhotoType() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PageBO<PhotoAlbumBO> pageBO = new PageBO<>();
        pageBO.setQuery(photoAlbumQTO);
        Integer count = photoAlbumDao.findForPageCount(photoAlbumQTO);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<PhotoAlbumBO> boList = PhotoAlbumBeanUtil.doList2BOList(photoAlbumDao.findForPage(photoAlbumQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(boList);

        return pageBO;
    }
}
